﻿using System;
using System.Collections.Generic;
using System.Linq;
using TESTCommons.ConvertTpye;
using TESTCommons.LinqUtil;
using TestDBData.EFContext;
using TestModel.SysManage;

namespace TestService.SysManage
{
    /// <summary>
    /// 功能
    /// </summary>
    public partial class Sys_MenuService : ISys_MenuService
    {
        #region 参数
        private readonly IRepository<Sys_Menu> _menuRepository;//菜单
        private readonly IRepository<Sys_MenuFunction> _FunRepository;//功能
        private readonly IRepository<Sys_Function> _sys_Function;//功能
        #endregion

        #region 构造函数
        public Sys_MenuService(IRepository<Sys_Menu> menuRepository,
            IRepository<Sys_MenuFunction> FunRepository, IRepository<Sys_Function> sys_Function)
        {
            _menuRepository = menuRepository;
            _FunRepository = FunRepository;
            _sys_Function = sys_Function;
        }
        #endregion

        /// <summary>
        /// 添加菜单和功能
        /// </summary>
        /// <param name="model">菜单</param>
        /// <param name="Funs">功能</param>
        public void AddMenu(Sys_Menu model, List<Sys_MenuFunction> functions)
        {
            _menuRepository.Commit(new Action(() =>
            {
                _menuRepository.Insert(model);
                if (functions != null && functions.Count() > 0)
                    _FunRepository.Insert(functions);
            }));
        }

        public void UpdateMenu(Sys_Menu model, List<Sys_MenuFunction> functions)
        {
            _menuRepository.Commit(new Action(() =>
            {

                _menuRepository.Update(model);
                var funs = _FunRepository.Table.Where(it => it.MenuFunction_MenuID == model.gid);
                _FunRepository.Delete(funs);
                if (functions != null && functions.Count() > 0)
                    _FunRepository.Insert(functions);
            }));
        }

        public void DeleteMenu(Sys_Menu model)
        {
            _menuRepository.Commit(new Action(() =>
            {
                _menuRepository.Delete(model);
                var funs = _FunRepository.Table.Where(it => it.MenuFunction_MenuID == model.gid);
                _FunRepository.Delete(funs);
            }));
        }

        public Sys_Menu GetModel(Guid gid)
        {
            return _menuRepository.GetById(gid);
        }

        public List<Sys_Menu> QueryMenu(string Menu_Name, string userName)
        {
            var expression = LinqExtensions.True<Sys_Menu>();
            if (!string.IsNullOrEmpty(Menu_Name))
            {
                expression = expression.And(t => t.Menu_Name == Menu_Name);
            }
            if (!string.IsNullOrEmpty(userName))
            {
                expression = expression.And(t => t.userName == userName);
            }
            return _menuRepository.Table.Where(expression.Compile()).ToList();
            //return new TestDBData.EFContext.PagedList<Sys_Menu>(menu, pageIndex, pageSize);
        }
        /// <summary>
        /// 获取功能
        /// </summary>
        public List<Sys_MenuFunction> GetMenuFunction(string fid)
        {
            var data = _FunRepository.Table.Join(_sys_Function.Table, fm => fm.MenuFunction_FunctionID, f => f.gid, (fm, f) => new Sys_MenuFunction()
            {
                gid = fm.gid,
                MenuFunction_MenuID = fm.MenuFunction_MenuID,
                MenuFunction_FunctionID = fm.MenuFunction_FunctionID,
                Function_Name = f.Function_Name,
                userName = fm.userName,
                createTime = fm.createTime
            }).Where(item => item.MenuFunction_MenuID == fid.ToGuid());
            return data.ToList();
        }
    }
}
